package Administrador.Interfaz;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class ConexionBD {

    private final String DRIVER = "org.postgresql.Driver";

    private final String NAME_DIR = "jdbc:postgresql://pgsql:5432/leydijch";
    private final String USER = "leydijch";
    private final String PASSWORD = "leydijch";
    private Connection conexion;
    
   /* private String dir = "jdbc:postgresql://localhost:5432/postgres";
    private String user = "postgres";
    private String pass = "yalile";*/
    

    public Connection conectar() {

        boolean errorCargandoDriver = false;
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "No se encontró el controlador", JOptionPane.ERROR_MESSAGE);
            errorCargandoDriver = true;
        }

        if (!errorCargandoDriver) {

            try {
                conexion = DriverManager.getConnection(NAME_DIR, USER, PASSWORD);
            } catch (SQLException se) {
                JOptionPane.showMessageDialog(null, se.getMessage(), "Error en base de datos!", JOptionPane.ERROR_MESSAGE);
            }
        }
       // System.out.println("conexion exitosa");
        return conexion;
    }// fin metodo conectar

    public void desconectarBaseDeDatos(Connection conexion) {

        try {
            conexion.close();
        } catch (Exception e) {
            System.err.println("No se pudo cerrar.");
        }
    }// fin del metodo desconectarDeBaseDatos
}
